﻿using HengWang.OA.Dal;
using HengWang.OA.Factory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace HengWang.OA.Bll
{
    public abstract class BaseBll<T> where T:class,new()
    {
        public DBSession DBSession
        {
            get
            {
                return new DBSession();
            }
        }
        public BaseDal<T> dal;
        public abstract void SetDal();
        public BaseBll(){
            SetDal();
    }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public T Add(T p)
        {
            return dal.Add(p);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public bool Update(T p)
        {
            return dal.Update(p);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public bool Delete(T p)
        {
            return dal.Delete(p);
        }
        /// <summary>
        /// 普通查询
        /// </summary>
        /// <param name="sewhere"></param>
        /// <returns></returns>
        public IEnumerable<T> Select(Expression<Func<T, bool>> sewhere)
        {
            return dal.Select(sewhere);
        }
        /// <summary>
        /// 查询单个
        /// </summary>
        /// <param name="sewhere"></param>
        /// <returns></returns>
        public T Single(Expression<Func<T, bool>> sewhere)
        {
            return dal.Single(sewhere);
        }
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <typeparam name="K">排序字段的类型</typeparam>
        /// <param name="sewhere">查询条件的lamda</param>
        /// <param name="orwhere">排序的lamda</param>
        /// <param name="pageindex">当前页</param>
        /// <param name="pagesize">每页显示条数</param>
        /// <param name="totalCount">数据总条数</param>
        /// <param name="pageCount">数据总页数</param>
        /// <returns>分页数据</returns>
        public IEnumerable<T> SelectPage<K>(Expression<Func<T, bool>> sewhere, Expression<Func<T, K>> orwhere, int pageindex, int pagesize, out int totalCount, out int pageCount)
        {
            totalCount = 0;
            pageCount = 0;
            return dal.SelectPage<K>(sewhere, orwhere, pageindex, pagesize, out totalCount, out pageCount);
        }
    }
}
